home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / shell / dialog-0.000 / dialog-0 / dialog-0.6c / dialog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-18  |  6.7 KB  |  267 lines

  1. /*
  2.  *  dialog - Display simple dialog boxes from shell scripts
  3.  *
  4.  *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
  5.  *
  6.  *  This program is free software; you can redistribute it and/or
  7.  *  modify it under the terms of the GNU General Public License
  8.  *  as published by the Free Software Foundation; either version 2
  9.  *  of the License, or (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include "dialog.h"
  22.  
  23. static void Usage (const char *name);
  24.  
  25. static int separate_output = 0;
  26.  
  27. typedef int (jumperFn) (const char *title, int argc, const char * const * argv);
  28.  
  29. struct Mode {
  30.     char *name;
  31.     int argmin, argmax, argmod;
  32.     jumperFn *jumper;
  33. };
  34.  
  35. jumperFn j_yesno, j_msgbox, j_infobox, j_textbox, j_menu;
  36. jumperFn j_checklist, j_radiolist, j_inputbox, j_guage;
  37.  
  38. /*
  39.  * All functions are used in the slackware root disk, apart from "guage"
  40.  */
  41.  
  42. static struct Mode modes[] =
  43. {
  44.     {"--yesno", 5, 5, 1, j_yesno},
  45.     {"--msgbox", 5, 5, 1, j_msgbox},
  46.     {"--infobox", 5, 5, 1, j_infobox},
  47.     {"--textbox", 5, 5, 1, j_textbox},
  48.     {"--menu", 8, 0, 2, j_menu},
  49.     {"--checklist", 9, 0, 3, j_checklist},
  50.     {"--radiolist", 9, 0, 3, j_radiolist},
  51.     {"--inputbox", 5, 6, 1, j_inputbox},
  52. #ifdef HAVE_GUAGE
  53.     {"--guage", 6, 6, 1, j_guage},
  54. #endif
  55.     {NULL, 0, 0, 0, NULL}
  56. };
  57.  
  58. static struct Mode *modePtr;
  59.  
  60. #ifdef LOCALE
  61. #include <locale.h>
  62. #endif
  63.  
  64. int
  65. main (int argc, const char * const * argv)
  66. {
  67.     int offset = 0, clear_screen = 0, end_common_opts = 0, retval;
  68.     const char *title = NULL;
  69.  
  70. #ifdef LOCALE
  71.     (void) setlocale (LC_ALL, "");
  72. #endif
  73.  
  74.     if (argc < 2) {
  75.     Usage (argv[0]);
  76.     exit (-1);
  77.     }
  78. #ifdef HAVE_RC_FILE
  79.  
  80.     else if (!strcmp (argv[1], "--create-rc")) {
  81. #ifdef HAVE_NCURSES
  82.     if (argc != 3) {
  83.         Usage (argv[0]);
  84.         exit (-1);
  85.     }
  86.     create_rc (argv[2]);
  87.     return 0;
  88. #else
  89.     fprintf (stderr, "This option is currently unsupported "
  90.          "on your system.\n");
  91.     return -1;
  92. #endif
  93.     }
  94. #endif
  95.  
  96.     while (offset < argc - 1 && !end_common_opts) {    /* Common options */
  97.     if (!strcmp (argv[offset + 1], "--title")) {
  98.         if (argc - offset < 3 || title != NULL) {
  99.         Usage (argv[0]);
  100.         exit (-1);
  101.         } else {
  102.         title = argv[offset + 2];
  103.         offset += 2;
  104.         }
  105.     } else if (!strcmp (argv[offset + 1], "--backtitle")) {
  106.         if (backtitle != NULL) {
  107.         Usage (argv[0]);
  108.         exit (-1);
  109.         } else {
  110.         backtitle = argv[offset + 2];
  111.         offset += 2;
  112.         }
  113.     } else if (!strcmp (argv[offset + 1], "--separate-output")) {
  114.         separate_output = 1;
  115.         offset++;
  116.     } else if (!strcmp (argv[offset + 1], "--clear")) {
  117.         if (clear_screen) {    /* Hey, "--clear" can't appear twice! */
  118.         Usage (argv[0]);
  119.         exit (-1);
  120.         } else if (argc == 2) {    /* we only want to clear the screen */
  121.         init_dialog ();
  122.         refresh ();    /* init_dialog() will clear the screen for us */
  123.         end_dialog ();
  124.         return 0;
  125.         } else {
  126.         clear_screen = 1;
  127.         offset++;
  128.         }
  129.     } else            /* no more common options */
  130.         end_common_opts = 1;
  131.     }
  132.  
  133.     if (argc - 1 == offset) {    /* no more options */
  134.     Usage (argv[0]);
  135.     exit (-1);
  136.     }
  137.     /* use a table to look for the requested mode, to avoid code duplication */
  138.  
  139.     for (modePtr = modes; modePtr->name; modePtr++)    /* look for the mode */
  140.     if (!strcmp (argv[offset + 1], modePtr->name))
  141.         break;
  142.  
  143.     if (!modePtr->name)
  144.     Usage (argv[0]);
  145.     if (argc - offset < modePtr->argmin)
  146.     Usage (argv[0]);
  147.     if (modePtr->argmax && argc - offset > modePtr->argmax)
  148.     Usage (argv[0]);
  149.     if ((argc - offset) % modePtr->argmod)
  150.     Usage (argv[0]);
  151.  
  152.     init_dialog ();
  153.     retval = (*(modePtr->jumper)) (title, argc - offset, argv + offset);
  154.  
  155.     if (clear_screen) {        /* clear screen before exit */
  156.     attr_clear (stdscr, LINES, COLS, screen_attr);
  157.     refresh ();
  158.     }
  159.     end_dialog();
  160.  
  161.     exit (retval);
  162. }
  163.  
  164. /*
  165.  * Print program usage
  166.  */
  167. static void
  168. Usage (const char *name)
  169. {
  170.     fprintf (stderr, "\
  171. \ndialog version 0.3, by Savio Lam (lam836@cs.cuhk.hk).\
  172. \n  patched to version %s by Stuart Herbert (S.Herbert@shef.ac.uk)\
  173. \n\
  174. \n* Display dialog boxes from shell scripts *\
  175. \n\
  176. \nUsage: %s --clear\
  177. \n       %s --create-rc <file>\
  178. \n       %s [--title <title>] [--separate-output] [--backtitle <backtitle>] [--clear] <Box options>\
  179. \n\
  180. \nBox options:\
  181. \n\
  182. \n  --yesno     <text> <height> <width>\
  183. \n  --msgbox    <text> <height> <width>\
  184. \n  --infobox   <text> <height> <width>\
  185. \n  --inputbox  <text> <height> <width> [<init>]\
  186. \n  --textbox   <file> <height> <width>\
  187. \n  --menu      <text> <height> <width> <menu height> <tag1> <item1>...\
  188. \n  --checklist <text> <height> <width> <list height> <tag1> <item1> <status1>...\
  189. \n  --radiolist <text> <height> <width> <list height> <tag1> <item1> <status1>...\
  190. \n  --guage     <text> <height> <width> <percent>\n", VERSION, name, name, name);
  191.     exit (-1);
  192. }
  193.  
  194. /*
  195.  * These are the program jumpers
  196.  */
  197.  
  198. int
  199. j_yesno (const char *t, int ac, const char * const * av)
  200. {
  201.     return dialog_yesno (t, av[2], atoi (av[3]), atoi (av[4]));
  202. }
  203.  
  204. int
  205. j_msgbox (const char *t, int ac, const char * const * av)
  206. {
  207.     return dialog_msgbox (t, av[2], atoi (av[3]), atoi (av[4]), 1);
  208. }
  209.  
  210. int
  211. j_infobox (const char *t, int ac, const char * const * av)
  212. {
  213.     return dialog_msgbox (t, av[2], atoi (av[3]), atoi (av[4]), 0);
  214. }
  215.  
  216. int
  217. j_textbox (const char *t, int ac, const char * const * av)
  218. {
  219.     return dialog_textbox (t, av[2], atoi (av[3]), atoi (av[4]));
  220. }
  221.  
  222. int
  223. j_menu (const char *t, int ac, const char * const * av)
  224. {
  225.     int ret = dialog_menu (t, av[2], atoi (av[3]), atoi (av[4]),
  226.             atoi (av[5]), (ac - 6) / 2, av + 6);
  227.     if (ret >= 0) {
  228.     fprintf(stderr, av[6 + ret * 2]);
  229.     return 0;
  230.     } else if (ret == -2)
  231.     return 1;    /* CANCEL */
  232.     return ret;        /* ESC pressed, ret == -1 */
  233. }
  234.  
  235. int
  236. j_checklist (const char *t, int ac, const char * const * av)
  237. {
  238.     return dialog_checklist (t, av[2], atoi (av[3]), atoi (av[4]),
  239.     atoi (av[5]), (ac - 6) / 3, av + 6, FLAG_CHECK, separate_output);
  240. }
  241.  
  242. int
  243. j_radiolist (const char *t, int ac, const char * const * av)
  244. {
  245.     return dialog_checklist (t, av[2], atoi (av[3]), atoi (av[4]),
  246.     atoi (av[5]), (ac - 6) / 3, av + 6, FLAG_RADIO, separate_output);
  247. }
  248.  
  249. int
  250. j_inputbox (const char *t, int ac, const char * const * av)
  251. {
  252.     int ret = dialog_inputbox (t, av[2], atoi (av[3]), atoi (av[4]),
  253.                 ac == 6 ? av[5] : (char *) NULL);
  254.     if (ret == 0)
  255.     fprintf(stderr, dialog_input_result);
  256.     return ret;
  257. }
  258.  
  259. #ifdef HAVE_GUAGE
  260. int
  261. j_guage (const char *t, int ac, const char * const * av)
  262. {
  263.     return dialog_guage (t, av[2], atoi (av[3]), atoi (av[4]),
  264.              atoi (av[5]));
  265. }
  266. #endif
  267.